package com.zc.core;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zc.common.Constants;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by zhangchi9 on 2016/11/10.
 */
@Configuration
@EnableWebMvc//激活Spring MVC 配置
@ComponentScan(basePackages = "com.zc.controller")
public class ApplicationWebMvcConfig extends WebMvcConfigurerAdapter {

    /*
	 * (non-Javadoc)
	 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
	 * #configureViewResolvers(
	 * org.springframework.web.servlet.config.annotation.ViewResolverRegistry)
	 * 配置视图解析翻译从控制器返回到具体的基于字符串的视图名称视图 实现执行与渲染。
	 */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.enableContentNegotiation(new MappingJackson2JsonView());
        registry.jsp("/WEB-INF/views/", ".jsp");//注册JSP视图解析器指定的前缀和后缀。
    }
    /*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addResourceHandlers(
	 * org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry)
	 * 添加处理程序服务静态资源，如图片，JS，并从其他受Web应用程序根目录的特定位置，类路径中，cssfiles。
	 */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/styles/**").addResourceLocations("/styles/");
        registry.addResourceHandler("/scripts/**").addResourceLocations("/scripts/");
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
    /*
	  * (non-Javadoc)
	  *
	  * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
	  * #addViewControllers( org.springframework.web.servlet.config.annotation.ViewControllerRegistry)
      *
	  * 配置视图控制器
	  */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("/login");
        registry.addViewController("/login.do").setViewName("/login");

    }
    /*
	   * (non-Javadoc)
	   *
	   * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
	   * #configureDefaultServletHandling(org.springframework.web.servlet.config.
	   * annotation.DefaultServletHandlerConfigurer)
	   */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#
     * configureMessageConverters(java.util.List)
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // Date to/from json
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat(Constants.DATEFORMAT));
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(objectMapper);
        converters.add(converter);
    }


}
